<template>
  <div>
    <el-button @click="send">发送webSocket</el-button>
  </div>
</template>

<script>
// import { doSend, contactSocket } from "@/utils/weibsocket.js";
export default {
  mounted() {},
  methods: {
    send() {
      if (window.WebSocket) {
        var ws = new WebSocket("ws://localhost:8888");

        ws.onopen = function () {
          console.log("连接服务器成功");
          // 向服务器发送消息
          ws.send("what`s your name?");
        };
        ws.onclose = function () {
          console.log("服务器关闭");
        };
        ws.onerror = function () {
          console.log("连接出错");
        };
        // 接收服务器的消息
        ws.onmessage = function (e) {
          console.log("e", e);
          let message = "message:" + e.data + "";
          console.log(message);
        };
      }
    },
  },
};
</script>

<style></style>
